function creatRequest(url, method, data, isLoading) {
  var header = ''
  var AMAP = 'https://www.szhongtu.net/scxmcm'
  if (method.toUpperCase() == 'POST') {
console.log('token>>',wx.getStorageSync('token'))
    header = {
      'Content-Type': 'application/x-www-form-urlencoded',
      'token': wx.getStorageSync('token')
    }
  } else {
    header = { 
      'content-type': 'application/json',
      'token': wx.getStorageSync('token')
    }
  }
  return new Promise((resolve, reject) => {
    if (!isLoading) {
      // wx.showLoading({
      //   title: '加载中'
      // })
      wx.showNavigationBarLoading();
    }
    wx.request({
      url: AMAP + url,
      data: data || {},
      header,
      method: method,
      success: function(res) {
        wx.hideLoading();
        wx.hideNavigationBarLoading();
        if (res.data.ret == 0) {
          resolve(res.data)
        } else if (res.data.ret == 1) {
          wx.showModal({
            title: '',
            content: res.data.msg
          })
          resolve(res.data)
        } else if (res.data.ret == 2) {
          wx.showToast({
            title: '登录失效,重新登录中',
            icon: 'none',
            duration: 2000
          })
          // 登录
          wx.login({
            success: res => {
              // 发送 res.code 到后台换取 openId, sessionKey, unionId
              console.log(res.code)
              wx.request({
                url: AMAP + '/public/index.php/wxroutine/index/login',
                data: {
                  code: res.code
                },
                header: {
                  'content-type': 'application/json'
                },
                success(ret) {
                  console.log(ret)
                  wx.setStorageSync('token', ret.data.data.token)
                  setTimeout(()=>{
                    wx.reLaunch({
                      url: '/pages/index/index'
                    })
                  },1000)
                
                }
              })

            }
          })
        } else {
          resolve(res.data)
        }
      },
      fail: function(err) {
        wx.hideLoading()
        wx.showToast({
          title: '网络异常，请稍后再试',
          icon: 'none'
        })
        reject(new Error(res))
      }
    })
  })
}
function creatRequest1(url, method, data, isLoading) {
  var header = ''
  var AMAP = 'https://www.szhongtu.net/scxmcm'
  if (method.toUpperCase() == 'POST') {
    console.log('token>>', wx.getStorageSync('token'))
    header = {
      'Content-Type': 'application/x-www-form-urlencoded',
      'token': wx.getStorageSync('token')
    }
  } else {
    header = {
      'content-type': 'application/json',
      'token': wx.getStorageSync('token')
    }
  }
  return new Promise((resolve, reject) => {
    if (!isLoading) {
      // wx.showLoading({
      //   title: '加载中'
      // })
      wx.showNavigationBarLoading();
    }
    wx.request({
      url: AMAP + url,
      data: data || {},
      header,
      method: method,
      success: function (res) {
        wx.hideLoading();
        wx.hideNavigationBarLoading();
        if (res.data.ret == 0) {
          resolve(res.data)
        } else if (res.data.ret == 1) {
          wx.showModal({
            title: '',
            content: res.data.msg
          })
          resolve(res.data)
        } else if (res.data.ret == 2) {
          wx.showToast({
            title: '登录失效,重新登录中',
            icon: 'none',
            duration: 2000
          })
          // 登录
          wx.login({
            success: res => {
              // 发送 res.code 到后台换取 openId, sessionKey, unionId
              console.log(res.code)
              wx.request({
                url: AMAP + '/public/index.php/wxroutine/index/login',
                data: {
                  code: res.code
                },
                header: {
                  'content-type': 'application/json'
                },
                success(ret) {
                  console.log(ret)
                  wx.setStorageSync('token', ret.data.data.token)
                  setTimeout(() => {
                    wx.reLaunch({
                      url: '/pages/index/index'
                    })
                  }, 1000)

                }
              })

            }
          })
        } else {
          resolve(res.data)
        }
      },
      fail: function (err) {
        wx.hideLoading()
        wx.showToast({
          title: '网络异常，请稍后再试',
          icon: 'none'
        })
        reject(new Error(res))
      }
    })
  })
}
function getToken(opt) {
  return creatRequest('/public/index.php/wxroutine/index/login', 'post', opt, true)
}

function getIndexBanner(opt) {
  return creatRequest1('/public/index.php/getbanner', 'get', opt, true)
}

function questionList(opt) {
  return creatRequest('/public/index.php/wxroutine/index/getNewstQuestion', 'get', opt)
}

function hotSearch(opt) {
  return creatRequest('/public/index.php/wxroutine/index/hotTopic', 'get', opt)
}
// 保存用户头像和昵称
function saveUserInfo(opt) {
  return creatRequest('/public/index.php/wxroutine/index/saveUserinfo', 'post', opt)
}

function publishQuestion(opt) {
  return creatRequest('/public/index.php/wxroutine/question/writequestion', 'post', opt)
}
//识别图片
function recognition(opt) {
  return creatRequest('/public/index.php/wxroutine/quanzi/signanimal', 'post', opt)
}
// 关注
function isCancer(opt) {
  return creatRequest('/public/index.php/wxroutine/index/cancerquestion', 'post', opt)
}
// 获取问题详情页评论
function getQuestionCommit(opt) {
  return creatRequest('/public/index.php/wxroutine/question/getQuetionreview', 'post', opt)
}

function questionDetail(opt) {
  return creatRequest('/public/index.php/wxroutine/question/getQuestion', 'post', opt)
}
// 关注
function isCancer(opt) {
  return creatRequest('/public/index.php/wxroutine/index/cancerquestion', 'post', opt)
}
// 获取问题详情页评论
function getQuestionCommit(opt) {
  return creatRequest('/public/index.php/wxroutine/question/getQuetionreview', 'post', opt)
}
// 问题详情页，点赞 功能
function like(opt) {
  return creatRequest('/public/index.php/wxroutine/question/questionzan', 'post', opt)
}
// 问题详情页，评论功能
function comment(opt) {
  return creatRequest('/public/index.php/wxroutine/question/writereview', 'post', opt)
}
// 圈子点赞
function cicrlComment(opt) {
  return creatRequest('/public/index.php/starmoments', 'post', opt)
}
// 评论所有回复
function allReply(opt) {
  return creatRequest('/public/index.php/wxroutine/question/getsonReview', 'post', opt)
}
//获取圈子内容
function getQuanzi(opt) {
  return creatRequest('/public/index.php/wxroutine/quanzi/getQuanzi', 'post', opt)
}
//发布圈子
function writearticl(opt) {
  return creatRequest('/public/index.php/wxroutine/quanzi/writearticl', 'post', opt)
}
//圈子详情
function getdetail(opt) {
  return creatRequest('/public/index.php/getmoments', 'post', opt)
}
//圈子评论
function getreviews(opt) {
  return creatRequest('/public/index.php/getreview', 'post', opt)
}
//回答问题
function answer(opt) {
  return creatRequest('/public/index.php/wxroutine/question/anwerquestion', 'post', opt)
}
//发布领养
function declareLingyang(opt) { 
  return creatRequest('/public/index.php/wxroutine/lingyang/declareLingyang', 'post', opt)
}
//获取附近
function getNearby(opt) {
  return creatRequest('/public/index.php/wxroutine/quanzi/getfaxian', 'post', opt)
}
//圈子发布评论或回复 
function addquanzireview(opt) {
  return creatRequest('/public/index.php/wxroutine/quanzi/addquanzireview', 'post', opt)
}
// 从圈子进入查看全部回复的接口
function lookAllReplyCircle(opt) {
  return creatRequest('/public/index.php/wxroutine/quanzi/getsonReview', 'post', opt)
}
//获取推荐/附近，热门
function getfaxian(opt) {
  return creatRequest('/public/index.php/wxroutine/quanzi/getfaxian', 'post', opt)
}
//获取我的关注，推荐，附近医生
function invitelist(opt) {
  return creatRequest('/public/index.php/wxroutine/User/invitelist', 'post', opt)
}
//获取发现首页头部
function faxiantop(opt) {
  return creatRequest('/public/index.php/wxroutine/quanzi/faxiantop', 'post', opt)
}
//我关注的圈子动态
function getcancerquanzi(opt) {
  return creatRequest('/public/index.php/wxroutine/quanzi/getcancerquanzi', 'post', opt)
}
//我加入的
function getMycircle(opt) {
  return creatRequest('/public/index.php/wxroutine/quanzi/getMycircle', 'post', opt)
}
//我关注的圈子动态
//关注
function cancerSome(opt) {
  return creatRequest('/public/index.php/wxroutine/User/cancerSome', 'post', opt)
}
function faxiandata(type){
  var type = type
  var page = page
  var limit = limit
  return type,page,limit;
}
// 上传资料
function upDoctorData(opt) {
  return creatRequest('/public/index.php/wxroutine/doctor/uploadshop', 'post', opt)
}
module.exports = {
  getToken: getToken,
  getIndexBanner: getIndexBanner,
  questionList: questionList,
  hotSearch: hotSearch,
  saveUserInfo: saveUserInfo,
  publishQuestion: publishQuestion,
  questionDetail: questionDetail,
  isCancer: isCancer,
  getQuestionCommit: getQuestionCommit,
  recognition: recognition,
  like: like,
  comment: comment,
  allReply: allReply,
  getQuanzi: getQuanzi,
  writearticl: writearticl,
  getdetail: getdetail,
  getreviews: getreviews,
  answer:answer,
  getNearby:getNearby,
  addquanzireview:addquanzireview,
  lookAllReplyCircle:lookAllReplyCircle,
  getfaxian: getfaxian,
  declareLingyang: declareLingyang,
  faxiandata: faxiandata,
  invitelist: invitelist,
  faxiantop: faxiantop,
  cancerSome: cancerSome,
  getcancerquanzi: getcancerquanzi,
  getMycircle: getMycircle,
  upDoctorData:upDoctorData,
  cicrlComment: cicrlComment
}
